<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="授权查看资源名称" prop="authSourceName">
        <el-input
          v-model="queryParams.authSourceName"
          placeholder="请输入授权查看资源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被授权用户名" prop="authorizedUserName">
        <el-input
          v-model="queryParams.authorizedUserName"
          placeholder="请输入被授权用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权用户" prop="authUserName">
        <el-input
          v-model="queryParams.authUserName"
          placeholder="请输入授权用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['authcodeInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['authcodeInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['authcodeInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['authcodeInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AuthcodeInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" align="center" label="序号"></el-table-column>
      <el-table-column label="授权用户" align="center" prop="authUserName" />
      <el-table-column label="授权类型" align="center" prop="authType" >
        <template slot-scope="scope">
          <span v-if="scope.row.authType == 0">查看数据通道</span>
          <span v-else-if="scope.row.authType == 1">查看数据产品</span>
          <span v-else-if="scope.row.authType == 2">授权注册数据通道</span>
          <span v-else-if="scope.row.authType == 4">授权注册产品</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="授权查看资源" align="center" prop="authSourceName" />
      <el-table-column label="授权对象类型" align="center" prop="authObjectType" >
        <template slot-scope="scope">
          <span v-if="scope.row.authObjectType == 0">数据用户</span>
          <span v-else-if="scope.row.authObjectType == 1">管理人员</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="授权码" align="center" prop="authCode" />
      <el-table-column label="被授权用户" align="center" prop="authorizedUserName" />
      <el-table-column label="截止时间" align="center" prop="deadline" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isDelete" >
        <template slot-scope="scope">
          <span v-if="scope.row.isDelete == 0">未删除</span>
          <span v-else-if="scope.row.isDelete == 1">已删除</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['authcodeInfo:edit']"
          >授权</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['authcodeInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['authcodeInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引擎信息信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="授权用户" prop="authUserId">
          <el-select v-model="form.authUserId" placeholder="请选择授权用户" >
            <el-option v-for="(item,index) in user" :key="index" :label="item.nickName" :value="item.userId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="授权类型" prop="authType">
          <el-select v-model="form.authType" placeholder="请输入适用数据通道种类" >
            <el-option label="查看数据通道" value="0" />
            <el-option label="查看数据产品" value="1" />
            <el-option label="授权注册数据通道" value="2" />
            <el-option label="授权注册产品" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="授权码" prop="authCode">
          <el-input v-model="form.authCode" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="授权查看资源ID" prop="authSourceId">
          <el-input v-model="form.authSourceId" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="授权对象类型" prop="authObjectType">
          <el-select v-model="form.authObjectType" placeholder="请输入授权对象类型" >
            <el-option label="数据用户" value="0" />
            <el-option label="管理人员" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="被授权用户" prop="authorizedUserId">
          <el-select v-model="form.authorizedUserId" placeholder="请选择被授权用户" >
            <el-option v-for="(item,index) in user" :key="index" :label="item.nickName" :value="item.userId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="截止时间" prop="deadline">
          <el-date-picker
            v-model="form.deadline"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 授权操作 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="授权类型" prop="authType">
          <el-select v-model="form.authType" placeholder="请输入适用数据通道种类" >
            <el-option label="查看数据通道" value="0" />
            <el-option label="查看数据产品" value="1" />
            <el-option label="授权注册数据通道" value="2" />
            <el-option label="授权注册产品" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="授权码" prop="authCode">
          <el-input v-model="form.authCode" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核结果" prop="auditResult">
          <el-input v-model="form.auditResult" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="授权" prop="status">
          <el-radio v-model="form.status" :label="0">通过</el-radio>
          <el-radio v-model="form.status" :label="1">拒绝</el-radio>
        </el-form-item>
         <el-form-item label="操作" prop="operateType">
          <el-radio v-model="form.operateType" :label="0">取消</el-radio>
          <el-radio v-model="form.operateType" :label="1">确定授权</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuthcodeInfo, getAuthcodeInfo, delAuthcodeInfo, addAuthcodeInfo, updateAuthcodeInfo,getApplicationUserInfo,listAllDataProduct1,operateAuthcodeInfo } from "@/api/qysj/app/AuthcodeInfo.js";

export default {
  name: "AuthcodeInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引擎信息信息表格数据
      AuthcodeInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 用户
      user:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authSourceName:'',
        authorizedUserName:'',
        authUserName:''
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        auditId: [
          { required: true, message: "审核Id不能为空", trigger: "blur" }
        ],
        createUserId: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询引擎信息信息列表 */
    getList() {
      this.loading = true;
      listAuthcodeInfo(this.queryParams).then(response => {
        this.AuthcodeInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createUserId: null,
        remark: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      getApplicationUserInfo().then(response => {
        this.user=response.rows
      });
      // listAllDataProduct1(this.queryParams1).then(response => {
      //   console.log(response);
      //   // this.user=response.rows
      // });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getUserList()
      this.reset();
      this.open = true;
      this.title = "添加授权信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getUserList()
      this.reset();
      const id = row.id || this.ids
      getAuthcodeInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改授权信息";
      });
    },
    /** 授权按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids
      getAuthcodeInfo(id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "授权信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuthcodeInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuthcodeInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 授权提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form1.id=this.form.id
            this.form1.auditResult=this.form.auditResult
            this.form1.authCode=this.form.authCode
            this.form1.authType=this.form.authType
            this.form1.operateType=this.form.operateType
            this.form1.status=this.form.status
            operateAuthcodeInfo(this.form1).then(response => {
              this.$modal.msgSuccess("授权成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除授权用户为"' + row.authUserName + '"的数据项？').then(function() {
        return delAuthcodeInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/auth/code/export', {
        ...this.queryParams
      }, `AuthcodeInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
